package com.yw.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yw.accountInfo.dto.LoginDTO;
import com.yw.accountInfo.vo.AccountInfoVO;
import com.yw.exception.BusinessException;
import com.yw.result.PageDTO;
import com.yw.result.PageVO;
import com.yw.result.R;
import com.yw.service.AccountInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Tag(name = "用户管理", description = "用户管理相关接口")
@RestController
@RequestMapping("/user")
public class AccountInfoController {
    @Resource
    private AccountInfoService accountInfoService;
    @Operation(summary = "用户登录", description = "用户登录接口")
    @PostMapping("/login")
    public R login(@Parameter(description = "登录信息") @RequestBody LoginDTO loginDto) {
        Boolean result = accountInfoService.login(loginDto);
        if (result) {
            // 登录成功，返回token和其他必要信息
            return R.OK( StpUtil.getTokenValue());
        }
        return R.FAIL("账号或密码错误");
    }
    @Operation(summary = "获取账户列表", description = "分页获取账户列表")
    @PostMapping("/getAccount")
    public R getAccount(@Parameter(description = "分页参数") @RequestBody @Validated PageDTO pageDTO) {
        try {
            Page<AccountInfoVO> pageVO = accountInfoService.getAccount(pageDTO);

            // 构造返回的分页数据
            PageVO<AccountInfoVO> result = new PageVO<>();
            result.setCurrent(pageVO.getCurrent());
            result.setSize(pageVO.getSize());
            result.setTotal(pageVO.getTotal());
            result.setPages(pageVO.getPages());
            result.setRecords(pageVO.getRecords());

            return R.OK(result);
        } catch (Exception e) {
            return R.FAIL("获取账户列表失败");
        }
    }
    @Operation(summary = "用户登出")
    @GetMapping("/logout")
    public R logout() {
        StpUtil.logout();
        return R.OK("登出成功");
    }
    @Operation(summary = "新增用户", description = "新增用户信息")
    @PostMapping("/add")
    public R add(@Parameter(description = "用户信息") @Validated(AccountInfoVO.Add.class) @RequestBody AccountInfoVO accountInfoVO) {
        try {
            accountInfoService.save(accountInfoVO);
            return R.OK("新增用户成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        } catch (Exception e) {
            return R.FAIL("新增用户失败");
        }
    }
    @Operation(summary = "修改用户")
    @PutMapping("/update")
    public R update(@Validated(AccountInfoVO.Update.class) @RequestBody AccountInfoVO accountInfoVO) {
        try {
            if (accountInfoVO.getId() == null) {
                return R.FAIL("用户ID不能为空");
            }
            accountInfoService.update(accountInfoVO);
            return R.OK("修改用户成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        } catch (Exception e) {
            return R.FAIL("修改用户失败");
        }
    }
}
